package cn.hrlly.tao2shou.utils;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * User: gengliang
 * Date: 2016-02-04
 * Time: 16:18
 */

public class GpsUtil {
    public AMapLocationClient mapLocationClient=null;
    public AMapLocationListener mapLocationListener;
    public AMapLocationClientOption mapLocationClientOption;
    public double longitude;
    public double lantitude;
    public Context context;

    public GpsUtil(Context context){
        this.context=context;
    }

    public void startLocation(){
        mapLocationClient=new AMapLocationClient(context);
        mapLocationClientOption=new AMapLocationClientOption();
        setMapOption();
        mapLocationListener=new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if(aMapLocation!=null){
                    if (aMapLocation.getErrorCode()==0){
                        longitude=aMapLocation.getLongitude();
                        lantitude=aMapLocation.getLatitude();
                    }else {
                        System.out.println(aMapLocation.getErrorCode()+"");
                    }
                }
            }
        };
        mapLocationClient.setLocationOption(mapLocationClientOption);
        mapLocationClient.setLocationListener(mapLocationListener);
        mapLocationClient.startLocation();
    }

    //配置参数
    public void setMapOption(){
        mapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mapLocationClientOption.setNeedAddress(true);
        mapLocationClientOption.setOnceLocation(false);
        mapLocationClientOption.setWifiActiveScan(true);
        mapLocationClientOption.setMockEnable(false);
        mapLocationClientOption.setInterval(6000);
    }

    public double returnLongitude(){
        return longitude;
    }

    public double returnLantitude(){
        return lantitude;
    }

    public void stopLocation(){
        mapLocationClient.stopLocation();
    }
}
